﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.Security;
using System.Web.UI.WebControls;
using DAL;
using BLL;

namespace IAATO
{
    public partial class NavireManagement : System.Web.UI.Page
    {
        protected NavireService navireService;
        protected CompagnieService compagnieService;
        protected CategorieService categorieService;

        protected void Page_Load(object sender, EventArgs e)
        {
            navireService = new NavireService(new NavireManager());
            compagnieService = new CompagnieService(new CompagnieManager());
            categorieService = new CategorieService(new CategorieManager());
            
            #region binding gridView

            List<NAVIRE> navires = navireService.getAllNavires();
            if (navires == null)
            {
                lblError.Text = "An error has occurred";
            }
            else if (navires.Count == 0)
            {
                lblError.Text = "No ship in the database";
                gdwNavires.DataBind();
            }

            else
            {
                lblError.Text = "";
                gdwNavires.DataSource = navires;
                gdwNavires.DataBind();
            }

            if (!IsPostBack)
            {
                ddlCompagnie.DataSource = compagnieService.getAllCompagnies();
                ddlCompagnie.DataTextField = "NOM";
                ddlCompagnie.DataValueField = "ID";
                ddlCompagnie.DataBind();

                ddlCat.DataSource = categorieService.getAllCategories();
                ddlCat.DataTextField = "NOM";
                ddlCat.DataValueField = "ID";
                ddlCat.DataBind();
            }

            #endregion


        }

        protected void gdwNavires_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void btnAjouter_Click(object sender, EventArgs e)
        {
            int nb_passagers = 0;
            int.TryParse(txtPassagers.Text, out nb_passagers);

            NAVIRE newNavire = navireService.createNavire(txtNom.Text, int.Parse(ddlCat.SelectedValue), nb_passagers, txtEmail.Text, txtTelephone.Text, int.Parse(ddlCompagnie.SelectedValue), txtImmatriculation.Text);

            string navireUserName = newNavire.NOM + "_" + newNavire.ID;
            Membership.CreateUser(navireUserName, navireUserName + "password", null);
            Roles.AddUserToRole(navireUserName, "Navire");

            Response.Redirect("NavireManagement.aspx");
        }


        protected void btnModifier_Click(object sender, EventArgs e)
        {
            int maxpassagers = 0;
            int.TryParse(txtPassagers.Text, out maxpassagers);
            navireService.updateNavire(int.Parse(idUpdate.Value), txtNom.Text, int.Parse(ddlCat.SelectedValue), maxpassagers, txtEmail.Text, txtTelephone.Text, int.Parse(ddlCompagnie.SelectedValue), txtImmatriculation.Text);
            
            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;

            Response.Redirect("NavireManagement.aspx");
        }

        protected void btnAnnuler_Click(object sender, EventArgs e)
        {
            btnModifier.Visible = false;
            btnAnnuler.Visible = false;
            btnAjouter.Visible = true;

            txtNom.Text = "";
            txtImmatriculation.Text = "";
            txtEmail.Text = "";
            txtPassagers.Text = "";
            txtTelephone.Text = "";
        }

        protected void update_Click(object sender, EventArgs e)
        {
            NAVIRE navire = navireService.getNavireById(int.Parse(((ImageButton)sender).CommandArgument));

            txtNom.Text = navire.NOM;
            txtImmatriculation.Text = navire.IMMATRICULATION;
            ddlCat.SelectedValue = "" + navire.CATEGORIEID;
            txtEmail.Text = navire.ADRESSE_EMAIL;
            txtPassagers.Text = "" + navire.MAX_PASSAGERS;
            txtTelephone.Text = "" + navire.NUM_TEL;
            ddlCompagnie.SelectedValue = "" + navire.COMPAGNIEID;

            btnAjouter.Visible = false;
            btnModifier.Visible = true;
            btnAnnuler.Visible = true;
            hdfScrollIntoView.Value = "True";

            idUpdate.Value = "" + navire.ID;
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            navireService.deleteNavire(int.Parse(((ImageButton)sender).CommandArgument));
            Response.Redirect("NavireManagement.aspx");
        }

        protected void gdwNavires_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdwNavires.DataSource = navireService.getAllNavires();
            gdwNavires.PageIndex = e.NewPageIndex;
            gdwNavires.DataBind(); 
        }

    }
}